import {Injectable} from '@angular/core';
import {PageInterface} from "../../app/app.component";
import {MePage} from "../../pages/me/me";
import {Nav, NavController} from "ionic-angular";
import {Roles} from "../../model/common/Roles.model";

let index: PageInterface = {title: '首页', name: 'IndexPage', icon: 'menu_index', isTabRoot: true};
let client: PageInterface = {title: '客户', name: 'ClientIndexPage', icon: 'menu_client', isTabRoot: true};
let increase: PageInterface = {title: '增员', name: 'TeamTeamIncreasePage', icon: 'menu_increase', isTabRoot: true};
let activity: PageInterface = {title: '活动', name: 'ActivityListPage', icon: 'menu_activity', isTabRoot: true};
let policy: PageInterface = {title: '保单', name: 'PolicyListPage', icon: 'menu_policy', isTabRoot: true};
let performance: PageInterface = {title: '业绩', name: 'MePage', icon: 'menu_performance', isTabRoot: true};
let team: PageInterface = {title: '团队', name: 'TeamIndexPage', icon: 'menu_team', isTabRoot: true};
let schedule: PageInterface = {title: '日程', name: 'ScheduleListPage', icon: 'menu_schedule', isTabRoot: true};
let webcast: PageInterface = {title: '直播', name: 'WebcastIndexPage', icon: 'menu_webcast', isTabRoot: true};
let information: PageInterface = {title: '资讯', name: 'InformationListPage', icon: 'menu_information', isTabRoot: true};
let message: PageInterface = {title: '消息', name: 'MessageOverviewPage', icon: 'menu_message', isTabRoot: true};
let setting: PageInterface = {title: '设置', name: 'SettingPage', icon: 'menu_setting', isTabRoot: true};

@Injectable()
export class AuthService {


    public getAuthLevel() {
        return localStorage.getItem('roles') === '6';
    }

    public getAuthAvator() {
        return localStorage.getItem('profileURL');
    }

    public getAuthName() {
        return localStorage.getItem('nickName');
    }

    //个险内勤、活动编辑员、超级管理员、团险内勤 不查看名单
    public showRoster() {
        return this.hasRole(Roles.Salesman) ||
            this.hasRole(Roles.SystemManager) ||
            this.hasRole(Roles.GroupManager) ||
            this.hasRole(Roles.GroupDirector);
    }

    public getNavMenu() {
        let roles = localStorage.getItem('roles');
        let agentLevel = localStorage.getItem('agentLevel');

        if (roles == '6' && agentLevel == 'FA') {
            return [index, client, policy, performance, increase,
                activity, webcast, information, message,
                schedule, setting];
        }

        if (roles == '6' && agentLevel != 'FA') {
            return [index, client, policy, performance, team,
                increase, activity, webcast, message,
                information, schedule, setting];
        }

        if (roles == '1') {
            return [index, team, activity, webcast, schedule,
                information, message, setting];
        }

        if (roles == '0' || roles == '2' || roles == '3' || roles == '4' || roles == '5') {
            return [index, activity, schedule, webcast, information,
                message, setting];
        }
    }

    /**
     * 判断当前是否在tab页之中
     */
    public canGoBack(navCtrl: NavController): boolean {
        return navCtrl.canGoBack();
    }

    private hasRole(role) {
        let roles = localStorage.getItem('fullRoles');
        let rolesArr = roles.split(',');
        let hasRole = false;
        rolesArr.forEach((item) => {
            if (item == role) {
                hasRole = true;
            }
        });
        return hasRole;
    }

}
